<template>
  <div class="comment">
    <div class="addcomment" v-show="!isFocus">
      <input type="text" placeholder="写跟帖" @focus="handlerFocus" />
      <span
        class="comment"
        @click="$router.push({ path: '/comment/' + post.id })"
      >
        <i class="iconfont iconpinglun-"></i>
        <em>{{ post.comment_length }}</em>
      </span>
      <i
        class="iconfont iconshoucang"
        @click="starArticle"
        :class="{ active: post.has_star }"
      ></i>
      <i class="iconfont iconfenxiang"></i>
    </div>
    <div class="inputcomment" v-show="isFocus">
      <textarea ref="commtext" rows="5"></textarea>
      <div>
        <span @click="sendComment">发 送</span>
        <span @click="cancelComment">取 消</span>
      </div>
    </div>
  </div>
</template>

<script>
import { post_star, replyComment } from "@/apis/article.js";
export default {
  props: {
    post: {
      required: true,
    },
    obj: {
      default: null,
    },
    watch: {
      //obj对象得变化就会触发监听
      obj() {
        // 弹出输入框，只有单击了回复，传递了对象数据的时候才真正的需要弹出输入框
        console.log("子组件中的代码", this.obj);
        if (this.obj) {
          this.isFocus = true;
          setTimeout(() => {
            this.$refs.commtext.placeholder = "@" + this.boj.user.nickname;
            this.$refs.commtext.focus();
          }, 20);
        }
      },
    },
  },
  data() {
    return {
      isFocus: false,
    };
  },
  methods: {
    //取消评论
    cancelComment() {
      // console.log(1312);
      //让输入框隐藏
      this.isFocus = false;
      // console.log(resetObj);
      //重置obj为Null ,只能让父组件进行Obj 得重置
      this.$emit("resetObj");
    },
    //发表评论
    async sendComment() {
      let id = this.$route.params.id;
      let data = {
        content: this.$refs.commtext.value,
      };
      if (data.content.trim().length == 0) {
        this.$toast.success("请输入评论内容");
        return;
      }
      //如果是单击恢复，就会有obj，同时也会有Id
      if (this.obj) {
        data.parent_id = this.obj.id;
      }
      let res = await replyComment(id, data);
      this.$toast.success(res.data.message);
      this.$refs.commtext.value = "";
      this.isFocus = false;
      // 发出事件 ，让父组件进行评论列表的数据刷新
      this.$emit("refresh ");
      // console.log(res);
    },
    // 实现文章收藏功能
    async starArticle() {
      let res = await post_star(this.post.id);

      this.post.has_star = !this.post.has_star;
      this.$toast.success(res.data.message);
    },
    //   获取焦点时触发
    handlerFocus() {
      this.isFocus = !this.isFocus;
      // this.$refs.commtext.focus();
      setTimeout(() => {
        this.$refs.commtext.focus();
      }, 20);
    },
  },
};
</script>

<style lang='less' scoped>
.comment {
  width: 100vw;
  position: fixed;
  left: 0;
  bottom: 0;
}
.inputcomment {
  padding: 10px;
  box-sizing: border-box;
  width: 100%;
  display: flex;
  background-color: #fff;
  align-items: flex-end;
  textarea {
    flex: 3;
    background-color: #eee;
    border: none;
    border-radius: 10px;
    padding: 10px;
  }
  div {
    padding: 20px;
  }
  span {
    display: block;
    flex: 1;
    height: 24px;
    line-height: 24px;
    padding: 0 10px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    font-size: 13px;
  }
}
.addcomment {
  width: 100%;
  box-sizing: border-box;
  padding: 10px;
  margin-top: 20px;
  background-color: #fff;
  display: flex;
  text-align: center;
  position: absolute;
  bottom: 0;
  left: 0;
  > input {
    flex: 4;
    height: 30px;
    line-height: 30px;
    border-radius: 15px;
    border: none;
    background-color: #eee;
    padding-left: 20px;
    font-size: 14px;
  }
  i {
    font-size: 20px;
  }
  > span {
    flex: 1;
    position: relative;
    > em {
      position: absolute;
      right: 0;
      top: -5px;
      font-size: 10px;
      background-color: #f00;
      color: #fff;
      border-radius: 5px;
      padding: 3px 5px;
    }
  }
  .iconshoucang {
    &.active {
      color: pink;
    }
  }
  > i {
    flex: 1;
  }
}
</style>
